package com.neusoft.neuiotms.dcam.fixed.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractModel;
import com.neusoft.bizcore.web.jpa.model.Comment;
import com.neusoft.neuiotms.dcam.common.model.Location;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyStatus;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
@Table(name = "neuiot_inventory_duty")
@org.hibernate.annotations.Table(appliesTo = "neuiot_inventory_duty", comment = "盘点任务表")
public class InventoryDuty extends AbstractModel {

    private static final long serialVersionUID = 1L;

    /** 盘点任务名称 */
    @NotBlank(message = "盘点任务名称不能为空")
    @Length(max = 100, message = "盘点任务名称不能超过{max}位")
    @Column(length = 100)
    @Comment("varchar(100) NOT NULL COMMENT '名称'")
    private String name;

    /** 盘点任务编码 */
    @NotBlank(message = "盘点任务编码不能为空")
    @Length(max = 255, message = "盘点任务编码不能超过{max}位")
    @Column(length = 255, unique = true)
    @Comment("varchar(255) NOT NULL COMMENT '编码'")
    private String code;

    /** 任务状态 */
    @Enumerated(EnumType.STRING)
    @NotNull(message = "任务状态不能为空")
    @Comment("varchar(255) NOT NULL COMMENT '计划状态'")
    private InventoryDutyStatus status = InventoryDutyStatus.created;

    /** 关联位置 */
    @ManyToMany(targetEntity = Location.class)
    @JoinTable(name = "inventory_ref_duty_location")
    @LazyCollection(LazyCollectionOption.FALSE)
    List<Location> locations = new ArrayList<>();

    /** 关联类型 */
    @ManyToMany(targetEntity = AssetType.class)
    @JoinTable(name = "inventory_ref_duty_type")
    @LazyCollection(LazyCollectionOption.FALSE)
    List<AssetType> assetTypes = new ArrayList<>();

    /** 关联部门 */
    @ManyToMany(targetEntity = ConnectionRelation.class, cascade = {CascadeType.REMOVE, CascadeType.MERGE })
    @JoinTable(name = "inventory_ref_duty_org")
    @LazyCollection(LazyCollectionOption.FALSE)
    List<ConnectionRelation> orgRelation = new ArrayList<>();

    /** 盘点人 */
    @Column(length = 20, nullable = false)
    @Comment("bigint(20) NOT NULL COMMENT '盘点人ID'")
    private Long userId;

    /** 盘点人姓名 */
    @Length(max = 255, message = "使用人不能超过{max}位")
    @Column(length = 255, nullable = false)
    @Comment("varchar(255) NOT NULL  COMMENT '盘点人姓名'")
    private String userName;

    /** 关联盘点结果 */
    @OneToMany(mappedBy = "inventoryDuty", cascade = {CascadeType.ALL }, fetch = FetchType.EAGER)
    @LazyCollection(LazyCollectionOption.FALSE)
    List<InventoryResult> dutyResults = new ArrayList<>();

    /** 关联计划 */
    @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
    InventoryPlan inventoryPlan;

    @Column(nullable = false)
    private Date createTime = new Date();

    @Column(nullable = false)
    private Date updateTime = new Date();

}
